package com.yj.springbootmybatis.rest;

import com.yj.springbootmybatis.pojo.bo.UserAddressBO;
import com.yj.springbootmybatis.pojo.vo.UserAddressVO;
import com.yj.springbootmybatis.service.UserAddressService;
import com.yj.springbootmybatis.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(value = "收货地址管理", tags = {"收货地址管理接口"})
@RequestMapping("address")
@RestController
public class AddressController {


    /**
     * 用户在确认订单页面，可以针对收货地址做如下操作：
     * 1.查询用户的所有收获地址列表
     * 2.新增收获地址
     * 3.删除收获地址
     * 4.修改收获地址
     * 5.设置默认地址
     */
    @Autowired
    UserAddressService userAddressService;

    @ApiOperation(value = "查询用户的所有收获地址列表")
    @PostMapping("list")
    public ResponseUtil list(@RequestParam @ApiParam("用户id") String userId) {
        if (StringUtils.isBlank(userId)) {
            return ResponseUtil.success().message("当前用户不存在");
        }
        List<UserAddressVO> list = userAddressService.queryAll(userId);
        return ResponseUtil.success().data(list);
    }

    @ApiOperation(value = "新增收获地址")
    @PostMapping("add")
    public ResponseUtil add(@RequestBody UserAddressBO userAddressBO) {
        if (!Optional.ofNullable(userAddressBO).isPresent()) {
            return ResponseUtil.success().message("请输入正确的地址");
        }
        userAddressService.addNewUserAddress(userAddressBO);
        return ResponseUtil.success();
    }

    @ApiOperation(value = "修改收获地址")
    @PostMapping("update")
    public ResponseUtil update(@RequestBody UserAddressBO userAddressBO) {
        if (!Optional.ofNullable(userAddressBO.getId()).isPresent()) {
            return ResponseUtil.success().message("收货地址格式错误！");
        }
        int result = userAddressService.updateUserAddress(userAddressBO);
        if (result <= 0) {
            return ResponseUtil.error();
        }
        return ResponseUtil.success();
    }

    @ApiOperation(value = "删除收获地址")
    @PostMapping("delete")
    public ResponseUtil delete(@RequestParam Integer id, @RequestParam String userId) {

        if (StringUtils.isBlank(String.valueOf(id)) || StringUtils.isBlank(userId)) {
            return ResponseUtil.error();
        }
        UserAddressBO userAddressBO = new UserAddressBO();
        userAddressBO.setId(id);
        userAddressBO.setUserId(userId);

        int result = userAddressService.deleteUserAddress(userAddressBO);
        if (result <= 0) {
            return ResponseUtil.error();
        }
        return ResponseUtil.success();
    }

    @ApiOperation(value = "设置为默认收获地址")
    @PostMapping("setDefault")
    public ResponseUtil setDefault(@RequestParam Integer id, @RequestParam String userId) {

        if (StringUtils.isBlank(String.valueOf(id)) || StringUtils.isBlank(userId)) {
            return ResponseUtil.error();
        }
        UserAddressBO userAddressBO = new UserAddressBO();
        userAddressBO.setId(id);
        userAddressBO.setUserId(userId);

        userAddressService.setDefaultUserAddress(userAddressBO);

        return ResponseUtil.success();
    }

}
